package cn.znzz.cjm.filter;

import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;

public class SmsCodeProvider implements AuthenticationProvider {
    private UserDetailsService userDetailsService;
    public void setUserDetailsService(UserDetailsService userDetailsService){
        this.userDetailsService = userDetailsService;
    }
    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        //调用自己UserDetailService中的loaduserbyusername方法
        //此时查询的时候  因为是借用用户名和密码的模式 而现在传过来的是电话码  所以要求名字就是电话号码
        //如果有名字和电话号码是不同字段的情况   可以先通过名字查询到用户 再获取电话号码进行比对
        UserDetails user = userDetailsService.loadUserByUsername(authentication.getPrincipal().toString());
        return new SmsCodeAuthenticationToken(user.getUsername(),user.getPassword(),user.getAuthorities());
    }

    @Override
    public boolean supports(Class<?> authentication) {
        return (SmsCodeAuthenticationToken.class
                .isAssignableFrom(authentication));
    }
}